/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.cmd;

import java.awt.Rectangle;
import java.util.*;

import self.gee.*;
import self.gee.editor.*;

public class SelectionSameSize implements ICommand {
  final static public int SIZE_AS_SMALLEST = 0;
  final static public int SIZE_AS_BIGGEST = 1;
  
  final static public int OVERALL = 0;
  final static public int WIDTH = 1;
  final static public int HEIGHT = 2;
  
  
  private IDiagramEditor  editor;
  private List            selection = new ArrayList();
  private int             type;
  private int             aspect;
  private boolean         hasRectResizing;
  
  private List            rectangularSel = new ArrayList();
  private List            originalBounds = new ArrayList();
  private Rectangle       targetBounds;
  
//  public SelectionSameSize( IDiagramEditor ed, List sel, int sizing, int asp ) {
  public SelectionSameSize( IDiagramEditor ed, int sizing, int asp ) {
    editor = ed;
//    selection.addAll( sel );
    ed.getSelectedElements( selection );
    type = sizing;
    aspect = asp;
    categoriseSelection();
    findTargetBounds();
  }
  
  public void execute() {
    resizeBounds( false );
  }

  public void undo() {
    resizeBounds( true );
  }  
  
  private void resizeBounds( boolean undo ) {
    if ( !hasRectResizing )
      return;
      
    final IDiagramLayer layer = editor.getSource().getContext();
    layer.beginUpdate();

    int max = rectangularSel.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IRectangularElement elem = (IRectangularElement) rectangularSel.get( cntr );
      Rectangle reset;
      if ( undo ) 
        reset = (Rectangle) originalBounds.get( cntr );
      else {
        reset = elem.getBounds();
        setRectSize( reset );
      }
      elem.setBounds( reset );
    }
    
    layer.endUpdate();
    editor.setSelectedElements( selection );
    editor.getSource().redraw();
  }

  private void categoriseSelection() {
    int max = selection.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) selection.get( cntr );
      if ( elem instanceof IRectangularElement ) 
        rectangularSel.add( elem );
    }
  }
  
  private void findTargetBounds() {
    int max = rectangularSel.size();
    if ( max < 2 )
      return;
    hasRectResizing = true;
    IRectangularElement elem = (IRectangularElement) rectangularSel.get( 0 );
    targetBounds = elem.getBounds();
    int tarSz = getRectSize( targetBounds );
    originalBounds.add( targetBounds.clone() );
    for ( int cntr = 1; cntr < max; cntr++ ) {
      elem = (IRectangularElement) rectangularSel.get( cntr );
      Rectangle currBnds = elem.getBounds();
      originalBounds.add( currBnds.clone() );
      int curSz = getRectSize( currBnds );
      if ( type == SIZE_AS_SMALLEST ) {
        if ( curSz < tarSz ) {
          tarSz = curSz;
          targetBounds = currBnds;
        }
      } else {
        if ( curSz > tarSz ) {
          tarSz = curSz;
          targetBounds = currBnds;
        }
      }
    }
  }
  
  private int getRectSize( Rectangle r ) {
    switch ( aspect ) {
      case WIDTH:   return r.width;
      case HEIGHT:  return r.height;
      default:
        return r.width * r.height;
    }
  }
  
  private void setRectSize( Rectangle r ) {
    switch ( aspect ) {
      case WIDTH:   r.width = targetBounds.width; break;
      case HEIGHT:  r.height = targetBounds.height; break;
      default:
        r.width = targetBounds.width; 
        r.height = targetBounds.height;
    }
  }
}